package com.crm.system.service.impl.productManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.productManager.CustomerMemberProduct;
import com.crm.model.entity.productManager.CustomerProduct;
import com.crm.model.entity.productManager.CustomerProductImportVO;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.contractReviewManager.ContractImportVO;
import com.crm.service.productManager.CustomerMemberProductDbService;
import com.crm.service.productManager.CustomerProductDbService;
import com.crm.system.service.productManager.CustomerProductImportService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author luojie
 * @date 2021/6/29 15:20
 */
@Service
public class CustomerProductImportServiceImpl implements CustomerProductImportService {

    @Autowired
    private CustomerMemberProductDbService customerMemberProductDbService;
    @Autowired
    private CustomerProductDbService customerProductDbService;

    @Override
    public JSONObject productImport(List<CustomerProductImportVO> contractImportVOList) throws ParseException {
        JSONObject jsonObject = new JSONObject();
        //会员产品重复
        List<Integer> memberProductCodeRepeatIdList = new ArrayList();
        //非会员产品重复
        List<Integer> productCodeRepeatIdList = new ArrayList();
        //会员产品List
        List<Integer> memberProductIdList = new ArrayList();
        //非会员产品List
        List<Integer> productIdList = new ArrayList();

        //失败List
        List<Integer> productFailIdList = new ArrayList();

        for (int i = 0; i < contractImportVOList.size(); i++) {

            //产品编码
            String productCode = contractImportVOList.get(i).getProductCode();
            //会员分类
            String productCategories = contractImportVOList.get(i).getProductCategories();


/*            if("会员".equals(productCategories)){
                //会员产品
                //判断产品编码是否有重复
                Boolean existMemberProductCode = customerMemberProductDbService.isExistMemberProductCode(productCode, Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
                if(!existMemberProductCode){
                    CustomerMemberProduct  customerMemberProduct = addMemberProductModel(contractImportVOList.get(i));
                    Boolean result = customerMemberProductDbService.insertCustomerMemberProduct(customerMemberProduct);
                    if(result){
                        memberProductIdList.add(i+2);
                    }else {
                        productFailIdList.add(i+2);
                    }
                }else {
                    memberProductCodeRepeatIdList.add(i+2);
                }

            }else {*/
                //非会员产品
                //判断非会员产品产品编码是否有重复
                if(!StringUtils.isBlank(productCode)){
                    Boolean existProductCode = customerProductDbService.isExistProductCode(productCode, Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
                    if(!existProductCode){
                        CustomerProduct customerProduct = addCustomerProduct(contractImportVOList.get(i));
                        Boolean result = customerProductDbService.insertCustomerProduct(customerProduct);
                        if(result){
                            productIdList.add(i+2);
                        }else {
                            productFailIdList.add(i+2);
                        }
                    }else {
                        productCodeRepeatIdList.add(i+2);
                    }
                }

  //          }
            jsonObject.put("memberProductCodeRepeatIdList",memberProductCodeRepeatIdList);
            jsonObject.put("productCodeRepeatIdList",productCodeRepeatIdList);
            jsonObject.put("memberProductIdList",productCodeRepeatIdList);
            jsonObject.put("productIdList",productIdList);
            jsonObject.put("productFailIdList",productFailIdList);
        }
        return jsonObject;
    }

    public CustomerMemberProduct addMemberProductModel(CustomerProductImportVO customerProductImportVO){
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CustomerMemberProduct customerMemberProduct = new CustomerMemberProduct();
        customerMemberProduct.setMemberProductId(CommonUtils.getGUID());
        customerMemberProduct.setMemberProductCode(customerProductImportVO.getProductCode());
        customerMemberProduct.setMemberCardName(customerProductImportVO.getProductName());
        if(!StringUtils.isBlank(customerProductImportVO.getProductMemberPrice())){
            customerMemberProduct.setSellingCardPrice(new BigDecimal(customerProductImportVO.getProductMemberPrice()));

        }
        //会员产品有效期
        //会员等级
        customerMemberProduct.setProductMembershipLevel(1);
        //产品停、启用状态
        if("已上架".equals(customerProductImportVO.getShelfStatus())){
            customerMemberProduct.setProductStatus("0");
        }else {
            customerMemberProduct.setProductStatus("1");
        }
        customerMemberProduct.setWhetherIncludedAchievement("0");
        customerMemberProduct.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        customerMemberProduct.setCreatePeopleId(userAccount.getAccountId());
        customerMemberProduct.setCreateTm(new Date());

        return customerMemberProduct;
    }

    public CustomerProduct addCustomerProduct(CustomerProductImportVO customerProductImportVO) throws ParseException {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CustomerProduct customerProduct = new CustomerProduct();
        customerProduct.setCustomerProductId(CommonUtils.getGUID());
        customerProduct.setProductCode(customerProductImportVO.getProductCode());
        customerProduct.setServiceClassification(customerProductImportVO.getServiceClassification());
        customerProduct.setProductTag(customerProductImportVO.getProductTag());
        customerProduct.setProductName(customerProductImportVO.getProductName());
        if(!StringUtils.isBlank(customerProductImportVO.getProductOriginalPrice())){
            customerProduct.setProductOriginalPrice(new BigDecimal(customerProductImportVO.getProductOriginalPrice()));
        }
        if(!StringUtils.isBlank(customerProductImportVO.getProductUnitPrice())){
            customerProduct.setProductUnitPrice(new BigDecimal(customerProductImportVO.getProductUnitPrice().replace("元","")));
        }
        if(!StringUtils.isBlank(customerProductImportVO.getPerformanceConversionRatio())){
            customerProduct.setPerformanceConversionRatio(new BigDecimal(customerProductImportVO.getPerformanceConversionRatio()));
        }
        if(!StringUtils.isBlank(customerProductImportVO.getProductMemberPrice())){
            customerProduct.setProductMemberPrice(new BigDecimal(customerProductImportVO.getProductMemberPrice()));

        }
        if(!StringUtils.isBlank(customerProductImportVO.getTopicId())){
            customerProduct.setTopicId(Integer.parseInt(customerProductImportVO.getTopicId()));

        }
        if(!StringUtils.isBlank(customerProductImportVO.getStudyPeriod())&&"期".equals(customerProductImportVO.getStudyPeriod().substring(1))){
            customerProduct.setStudyPeriod(customerProductImportVO.getStudyPeriod().substring(0));
        }

        if(!StringUtils.isBlank(customerProductImportVO.getStudyPeriod())){
            //兑换方式
            switch(customerProductImportVO.getStudyPeriod()){
                case "充值码" :
                    customerProduct.setExchangeMethod("4");
                    break;
                case "兑换码" :
                    customerProduct.setExchangeMethod("2");
                    break;
                case "税法直通车学习卡卡号" :
                    customerProduct.setExchangeMethod("1");
                    break;
                case "学习码" :
                    customerProduct.setExchangeMethod("3");
                    break;
                default :
            }
        }

        //上架状态
        if("已上架".equals(customerProductImportVO.getShelfStatus())){
            customerProduct.setShelfStatus("1");
        }else {
            customerProduct.setShelfStatus("2");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //上架时间
        if(!StringUtils.isBlank(customerProductImportVO.getShelfTime())){
            Date aaa = simpleDateFormat.parse(customerProductImportVO.getShelfTime());
            customerProduct.setShelfTime(aaa);
        }
        //下架时间
        if(!StringUtils.isBlank(customerProductImportVO.getOffShelfTime())){
            customerProduct.setOffShelfTime(simpleDateFormat.parse(customerProductImportVO.getOffShelfTime()));
        }
        //兑换方式
        if(!StringUtils.isBlank(customerProductImportVO.getRedemptionCode())
                &&customerProductImportVO.getRedemptionCode().indexOf("兑换码")!=-1){
            customerProduct.setExchangeMethod("2");
        }
        customerProduct.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        customerProduct.setCreatePeopleId(userAccount.getAccountId());
        customerProduct.setCreateTm(new Date());
        return customerProduct;
    }
}
